<?php
/**
 * Created by PhpStorm.
 * User: 丶晓
 * Date: 2017/9/22
 * Time: 18:06
 */

namespace app\api\model;


use think\Db;
use think\Model;

class PayModel extends Model
{

    // 查
    public function selectInfo($type, $data = [])
    {
        if (empty($type)) {
            return false;
        }
        switch ($type) {
            case 'user_got_tempalte':
                return empty($data['uid']) or empty($data['template_id']) ? false : Db::table('wyz_template_order')
                        ->where(['uid' => $data['uid'], 'template_id' => $data['template_id']])
                        ->value('is_pay');
                break;
        }
        return false;
    }

    // 增
    public function addInfo($type, $data = [])
    {
        if (empty($type) or empty($data)) {
            return false;
        }
        switch ($type) {
            case 'int_recharge':
                return Db::table('wyz_recharge_order')->insertGetId($data);
                break;
            case 'int_buyTemplate':
                return Db::table('wyz_template_order')->insertGetId($data);
                break;
            case 'ready_alipay':
                unset($data['order_id']);
                return Db::table('wyz_ali_pay')->insertGetId($data);
                break;
            case 'ready_wechat':
                unset($data['order_id']);
                return Db::table('wyz_wechat_pay')->insertGetId($data);
                break;
            case 'ready_miniapps':
                return Db::table('wyz_miniapps_pay')->insert($data);
                break;
            case 'mchPayFile':
                $Mch_info = Db::table('wyz_miniapps')->field(['id', 'uid'])->where('AppId', $data['appid'])->select()[0];
                $this->deleteInfo('mchPayFile', ['miniapp_id' => $Mch_info['id']]);
                if ($Mch_info) {
                    if (Db::table('wyz_cert')->insert([
                            'miniapp_id' => trim($Mch_info['id']),
                            'uid' => trim($Mch_info['uid']),
                            'api' => trim($data['secret']),
                            'mch_id' => trim($data['mch_id']),
                            'wx_cert' => trim($data['cert']),
                            'wx_key' => trim($data['key'])
                        ]) == 1) {
                        return true;
                    }
                }
        }
        return false;
    }

    // 改
    public function changeInfo($type, $data)
    {
        if (empty($type) or empty($data)) {
            return false;
        }
        switch ($type) {
            case 'ready_buyTemplate':
                $datas = isset($data['ali_pay_id']) ? ['ali_pay_id' => $data['ali_pay_id']] : ['wechat_pay_id' => $data['wechat_pay_id']];
                if (Db::table('wyz_template_order')->where('id', $data['id'])->update($datas) == 1) {
                    return true;
                }
                break;
            case 'ready_payRecharge':
                $datas = isset($data['ali_pay_id']) ? ['ali_pay_id' => $data['ali_pay_id']] : ['wechat_pay_id' => $data['wechat_pay_id']];
                if (Db::table('wyz_recharge_order')->where('id', $data['id'])->update($datas) == 1) {
                    return true;
                }
                break;
            case 'ready_alipay_order':
                $where['out_trade_no'] = $data['out_trade_no'];
                unset($data['out_trade_no']);
                if (Db::table('wyz_ali_pay')->where($where)->update($data) == 1) {
                    return true;
                }
                break;
            case 'success_alipay_order':
                if (Db::table('wyz_ali_pay')->where($data)->update(['is_sign' => 1]) == 1) {
                    return true;
                }
                break;
            case 'success_wechatPay_order':
                $where['out_trade_no'] = $data['out_trade_no'];
                unset($data['out_trade_no']);
                if (Db::table('wyz_wechat_pay')->where($where)->update($data) == 1) {
                    return true;
                }
                break;
            case 'success_miniapps_pay':
                $where['out_trade_no'] = $data['out_trade_no'];
                unset($data['out_trade_no']);
                if (Db::table('wyz_miniapps_pay')->where($where)->update($data) == 1) {
                    return true;
                }
                break;
            case 'mchPayFile':
                $Mch_info = Db::table('wyz_miniapps')->field(['id', 'uid'])->where('AppId', $data['appid'])->select()[0];
                if ($Mch_info and Db::table('wyz_cert')->where(['miniapp_id' => $Mch_info['id'], 'uid' => $Mch_info['uid']])->count()) {
                    if (Db::table('wyz_cert')->where(['miniapp_id' => $Mch_info['id'], 'uid' => $Mch_info['uid']])->update([
                            'mch_id' => trim($data['mch_id']),
                            'api' => trim($data['secret']),
                            'wx_cert' => trim($data['cert']),
                            'wx_key' => trim($data['key'])
                        ]) == 1) {
                        return true;
                    }
                } else {
                    return $this->addInfo('mchPayFile', $data);
                }
                break;
        }
        return false;
    }

    // 删
    public function deleteInfo($type, $data)
    {
        if (empty($type) or empty($data)) {
            return false;
        }
        switch ($this) {
            case 'mchPayFile':
                return Db::table('wyz_cert')->where($data)->delete();
        }
        return false;

    }
}